package com.xhj.util;

import com.alibaba.fastjson.JSONObject;
import com.xhj.entity.Permission;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class AclUtils {

    /**
     * 使用构建树形菜单
     * @param list
     * @return
     */
    public static List<Permission> build(List<Permission> list){
        ArrayList<Permission> buildList = new ArrayList<>();
        // 构建一个map根据id存放Permission
        Map<String, Permission> permissionMap = new HashMap<>();
        for (Permission permission : list) {
            // 将permission存入permissionMap中
            permissionMap.put(permission.getId(), permission);
        }
        for (Permission permission : list) {
            // 假如pid为0说明是一级菜单
            if ("0".equals(permission.getPid())) {
                permission.setLevel(1);
                buildList.add(permission);
            }else {
                // 假如pid不为0
                Permission parentPermission = permissionMap.get(permission.getPid());
                // 初始化children数组
                if (parentPermission.getChildren() == null) parentPermission.setChildren(new ArrayList<>());
                if (parentPermission.getLevel() == null) {
                    permission.setLevel(4);
                }else {
                    permission.setLevel(parentPermission.getLevel() + 1);
                }
                parentPermission.getChildren().add(permission);
            }
        }
        return buildList;
    }

    /*public static List<Permission> build(List<Permission> treeNodes) {
        List<Permission> trees = new ArrayList<>();
        for (Permission treeNode : treeNodes) {
            if ("0".equals(treeNode.getPid())) {
                treeNode.setLevel(1);
                trees.add(findChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }*/

    /**
     * 递归查找子节点
     * @paramtreeNodes
     * @return
     */
    /*public static Permission findChildren(Permission treeNode,List<Permission> treeNodes) {
        treeNode.setChildren(new ArrayList<Permission>());

        for (Permission it : treeNodes) {
            if(treeNode.getId().equals(it.getPid())) {
                int level = treeNode.getLevel() + 1;
                it.setLevel(level);
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<>());
                }
                treeNode.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return treeNode;
    }*/

    public static void findIds(Permission permission,List<String> list) {
        List<Permission> children = permission.getChildren();
        // 判断是否有下属
        if (children != null && children.size() > 0) {
            for (Permission child : children) {
                list.add(child.getId());
                // 递归查找
                findIds(child,list);
            }
        }
    }

    public static List<JSONObject> buildJsonList(List<Permission> treeNodes) {
        List<JSONObject> menus = new ArrayList<>();
        if(treeNodes.size() == 1) {
            Permission topNode = treeNodes.get(0);
            //左侧一级菜单
            List<Permission> oneMenuList = topNode.getChildren();
            for(Permission one :oneMenuList) {
                JSONObject oneMenu = new JSONObject();
                oneMenu.put("path", one.getPath());
                oneMenu.put("component", one.getComponent());
                oneMenu.put("redirect", "noredirect");
                oneMenu.put("name", "name_"+one.getId());
                oneMenu.put("hidden", false);

                JSONObject oneMeta = new JSONObject();
                oneMeta.put("title", one.getName());
                oneMeta.put("icon", one.getIcon());
                oneMenu.put("meta", oneMeta);

                List<JSONObject> children = new ArrayList<>();
                List<Permission> twoMenuList = one.getChildren();
                for(Permission two :twoMenuList) {
                    JSONObject twoMenu = new JSONObject();
                    twoMenu.put("path", two.getPath());
                    twoMenu.put("component", two.getComponent());
                    twoMenu.put("name", "name_"+two.getId());
                    twoMenu.put("hidden", false);

                    JSONObject twoMeta = new JSONObject();
                    twoMeta.put("title", two.getName());
                    twoMenu.put("meta", twoMeta);

                    children.add(twoMenu);

//                    List<JSONObject> subChildren = new ArrayList<>();
                    List<Permission> threeMenuList = two.getChildren();
                    if (threeMenuList != null && threeMenuList.size() > 0) {
                        for (Permission three : threeMenuList) {
                            if (StringUtils.isEmpty(three.getPath())) continue;

                            JSONObject threeMenu = new JSONObject();
                            threeMenu.put("path", three.getPath());
                            threeMenu.put("component", three.getComponent());
                            threeMenu.put("name", "name_" + three.getId());
                            // 动态路由有懒加载
                            threeMenu.put("hidden", true);

                            JSONObject threeMeta = new JSONObject();
                            threeMeta.put("title", three.getName());
                            threeMenu.put("meta", threeMeta);

                            children.add(threeMenu);
                        }
//                        twoMenu.put("children",subChildren);
                    }
                }
                oneMenu.put("children", children);
                menus.add(oneMenu);
            }
        }
        return menus;
    }

    /*public static List<JSONObject> buildJsonList(List<Permission> treeNodes) {
        List<JSONObject> menus = new ArrayList<>();
        if(treeNodes.size() == 1) {
            Permission topNode = treeNodes.get(0);
            //左侧一级菜单
            List<Permission> oneMenuList = topNode.getChildren();
            for(Permission one :oneMenuList) {
                JSONObject oneMenu = new JSONObject();
                oneMenu.put("path", one.getPath());
                oneMenu.put("component", one.getComponent());
                oneMenu.put("redirect", "noredirect");
                oneMenu.put("name", "name_"+one.getId());
                oneMenu.put("hidden", false);

                JSONObject oneMeta = new JSONObject();
                oneMeta.put("title", one.getName());
                oneMeta.put("icon", one.getIcon());
                oneMenu.put("meta", oneMeta);

                List<JSONObject> children = new ArrayList<>();
                List<Permission> twoMenuList = one.getChildren();
                for(Permission two :twoMenuList) {
                    JSONObject twoMenu = new JSONObject();
                    twoMenu.put("path", two.getPath());
                    twoMenu.put("component", two.getComponent());
                    twoMenu.put("name", "name_"+two.getId());
                    twoMenu.put("hidden", false);

                    JSONObject twoMeta = new JSONObject();
                    twoMeta.put("title", two.getName());
                    twoMenu.put("meta", twoMeta);

                    children.add(twoMenu);

                    List<Permission> threeMenuList = two.getChildren();
                    if (threeMenuList != null && threeMenuList.size() > 0) {
                        for (Permission three : threeMenuList) {
                            if (StringUtils.isEmpty(three.getPath())) continue;

                            JSONObject threeMenu = new JSONObject();
                            threeMenu.put("path", three.getPath());
                            threeMenu.put("component", three.getComponent());
                            threeMenu.put("name", "name_" + three.getId());
                            threeMenu.put("hidden", true);

                            JSONObject threeMeta = new JSONObject();
                            threeMeta.put("title", three.getName());
                            threeMenu.put("meta", threeMeta);

                            children.add(threeMenu);
                        }
                    }
                }
                oneMenu.put("children", children);
                menus.add(oneMenu);
            }
        }
        return menus;
    }*/

}
